<?


require_once('Zend/Loader.php');
		
require_once('Zend/Gdata/YouTube.php');
require_once('Zend/Gdata/AuthSub.php');
require_once('Zend/Gdata/App/Exception.php');
require_once('Zend/Gdata/App/HttpException.php');		
		

class videoModel extends db implements db_interface{

	private static $instance;
	private $fotos = array();
	private $youtubeClient;
	
	public function __construct($iDb){
		
			parent::__construct($iDb);
			
			
			
			
	}
	
	public static function getInstance($iDb){
		
			if(self::$instance == NULL){
				self::$instance = new self($iDb);
			}
			
			return self::$instance;
		
		
	}
	
	
	
	public function insert(item_video $video){
			try{
			$httpClient = Zend_Gdata_AuthSub::getHttpClient('CJyk0PLPExCWpaid_f____8B');
			$clientId = 'ytapi-jelleversele-inabuurt-5r6l4fuk-0';
			$developerKey = 'AI39si5KTdX7ZEE3vzJgIKTwSSUGbG7iewWAycTCNsqQ_3B2UGYduUJC6s72xIt-IFCGPCtMm7KvEusKLdEWjtASPmbwmwz8Hg';
			
			$yt = new Zend_Gdata_YouTube($httpClient, 'inabuurt', $clientId , $developerKey);
			
			$myVideoEntry = new Zend_Gdata_YouTube_VideoEntry();
	
			$filesource = $yt->newMediaFileSource($video -> video['tmp_name']);
			$filesource->setContentType($video -> video['type']);
			$filesource->setSlug($video -> video['tmp_name']);
	
			$myVideoEntry->setMediaSource($filesource);
			
			$myVideoEntry->setVideoTitle($video -> title);
			
			$description = "Video is geupload door: " . $video -> user -> titel;
			
			$myVideoEntry->setVideoDescription($description);
			$myVideoEntry->setVideoCategory('Comedy');
			
			
			$myVideoEntry->SetVideoTags('inabuurt, ' . $video -> user -> titel);
			
			// Optionally set some developer tags
			$myVideoEntry->setVideoDeveloperTags(array('mydevelopertag',
													   'anotherdevelopertag'));
			
			
			$uploadUrl =
				'http://uploads.gdata.youtube.com/feeds/users/default/uploads';
			
			
			
			try {
				$newEntry = $yt->insertEntry($myVideoEntry,
											 $uploadUrl,
											 'Zend_Gdata_YouTube_VideoEntry');
											 
				
			} catch (Zend_Gdata_App_HttpException $httpException) {
				return false;
			} catch (Zend_Gdata_App_Exception $e) {
				return false;
			}		
			
			
			}catch(Zend_Http_Exception $e){
			
				return false;
			
			}
			
			if($newEntry -> getVideoId() != ''){
			
			
					
						$query = $this -> db -> prepare('CALL videoAdd(?,?,?,?)');
						
						$this -> bindVars($query, array($newEntry -> getVideoId(), $video -> title, $video -> user -> id, $video -> album_id));
						
						$query -> execute();
						
						return true;
						
						
				
						
			}else{
			
				return false;
			}
			
			
			
			
		return false;
		
	}
	
	
	
		public function getLastVideos()
	{
		$query = $this -> db -> query('SELECT i.item_id,bg.bew_titel,g.geb_id,i.item_title,v.video_youtube_id,DATE_FORMAT(i.item_datum, "%H:%i - %d/%m/%y") as date FROM item i join gebruikers g on (i.item_geb_id = g.geb_id) join bewonersgroep bg on (bg.bew_id = g.geb_id) join video v on (i.item_id = v.video_id) where i.item_deleted = 1 AND TIMEDIFF(now(),i.item_datum) > "00:30:00" order by item_id desc limit 5;');
		
		$videos = array();
		
		
		while ($row = $query->fetch_assoc())
		{
			$video = new item_video($this -> db);
			$video -> id = $row['item_id'];
			$video -> title = $row['item_title'];
			$video -> geb_naam = $row['bew_titel'];
			$video -> youtube_id = $row['video_youtube_id'];
			$video -> geb_id = $row['geb_id'];
			$video -> datum = $row['date'];
			
	
				$videos[] = $video;
			
		}
		
		return $videos;
	}
	
	
	
	
	
	public function getThumb(item_video $video){
	
	
		
	$video -> thumb = 'http://i3.ytimg.com/vi/'.$video -> youtube_id.'/1.jpg';

	
	}
	
	
	


			public function getVideosByUser(user_bewonersgroep $iUser)
	{
		$query = $this -> db -> prepare('SELECT i.item_id,bg.bew_titel,i.item_title,v.video_youtube_id, v.video_album_id, DATE_FORMAT(i.item_datum, "%H:%i - %d/%m/%y") as date FROM item i join gebruikers g on (i.item_geb_id = g.geb_id) join bewonersgroep bg on (bg.bew_id = g.geb_id) join video v on (i.item_id = v.video_id) where geb_id = ? AND i.item_deleted = 1 AND TIMEDIFF(now(),i.item_datum) > "00:30:00" order by item_id desc;');
		
		$videos = array();
		
		$bindValue = $iUser -> id;
		
		$query -> bind_param('i', $bindValue);
		
		$query -> execute();
		
		$query -> bind_result($item_id,$geb_naam,$item_title,$youtube_id,$date, $album); 
		
		while ($query -> fetch())
		{
			$video = new item_video($this -> db);
			$video -> id = $item_id;
			$video -> title = $item_title;
			$video -> geb_naam = $geb_naam;
			$video -> youtube_id = $youtube_id;
			$video -> user = $iUser;
			$video -> datum = $date;
			$video -> album_id = $album;
			
		
				$videos[] = $video;
			
		}
		
		return $videos;
	}
		public function delVideos(item_video $video)
	{
	
		$query = $this -> db -> prepare('call itemDel(?);');
		$bindValue = $video -> id;
		$query -> bind_param('i', $bindValue);
		$query -> execute();
		echo $video -> youtube_id;
		
		$httpClient = Zend_Gdata_AuthSub::getHttpClient('CJyk0PLPExCWpaid_f____8B');
			$clientId = 'ytapi-jelleversele-inabuurt-5r6l4fuk-0';
			$developerKey = 'AI39si5KTdX7ZEE3vzJgIKTwSSUGbG7iewWAycTCNsqQ_3B2UGYduUJC6s72xIt-IFCGPCtMm7KvEusKLdEWjtASPmbwmwz8Hg';
			
			$yt = new Zend_Gdata_YouTube($httpClient, 'inabuurt', $clientId , $developerKey);
			
		$entry = $yt->getFullVideoEntry($video -> youtube_id);	
			
		
			
		$yt->delete($entry);
			
			
			
		
	}
	
	
			public function getVideosByUserForEditList(user_bewonersgroep $iUser)
	{
		$query = $this -> db -> prepare('SELECT i.item_id,bg.bew_titel,i.item_title,v.video_youtube_id, v.video_album_id, DATE_FORMAT(i.item_datum, "%H:%i - %d/%m/%y") as date FROM item i join gebruikers g on (i.item_geb_id = g.geb_id) join bewonersgroep bg on (bg.bew_id = g.geb_id) join video v on (i.item_id = v.video_id) where geb_id = ? AND i.item_deleted = 1 order by item_id desc;');
		
		$videos = array();
		
		$bindValue = $iUser -> id;
		
		$query -> bind_param('i', $bindValue);
		
		$query -> execute();
		
		$query -> bind_result($item_id,$geb_naam,$item_title,$youtube_id,$date, $album); 
		
		while ($query -> fetch())
		{
			$video = new item_video($this -> db);
			$video -> id = $item_id;
			$video -> title = $item_title;
			$video -> geb_naam = $geb_naam;
			$video -> youtube_id = $youtube_id;
			$video -> user = $iUser;
			$video -> datum = $date;
			$video -> album_id = $album;
			
		
				$videos[] = $video;
			
		}
		
		return $videos;
	}
		
	

		public function fill(item_video $video)
	{
	
		$query = $this -> db -> prepare('SELECT i.item_id,bg.bew_titel,bg.bew_id,i.item_title,v.video_youtube_id, v.video_album_id, (SELECT item_title FROM item WHERE item_id = v.video_album_id) as album_naam,DATE_FORMAT(i.item_datum, "%H:%i - %d/%m/%y") as date FROM item i join gebruikers g on (i.item_geb_id = g.geb_id) join bewonersgroep bg on (bg.bew_id = g.geb_id) join video v on (i.item_id = v.video_id) where item_id = ? AND i.item_deleted = 1 AND TIMEDIFF(now(),i.item_datum) > "00:30:00" order by item_id desc;');
		
		
		
		$bindValue = $video -> id;
		
		$query -> bind_param('i', $bindValue);
		
		$query -> execute();
		
		$query -> bind_result($item_id,$geb_naam,$geb_id,$item_title,$youtube_id, $album_naam, $album,$date); 
		
		$query -> fetch();
		
		
			$video -> id = $item_id;
			$video -> geb_id = $geb_id;
			$video -> title = $item_title;
			$video -> geb_naam = $geb_naam;
			$video -> youtube_id = $youtube_id;
			$video -> datum = $date;
			$video -> album_id = $album;
			$video -> album_naam = $album_naam;

	}
	public function updVideo(item_video $video){
		$query = $this -> db -> prepare('call videoUpd(?,?,?);');
		$bindingVars = array($video -> title,$video -> id,$video -> album_id);
		$this -> bindVars($query,$bindingVars);
		$query -> execute();
	
	
	
	}	
	

	
	
	
	
}



?>